const updateFoodController = require('./updateFood.controller');
const addFoodController = require('./addFood.controller');

foodCtroller.$inject = [
    '$rootScope',
    '$scope',
    '$location',
    //'$routeParams',
    'foodService',
    '$uibModal',
    '$log'
]
 function foodCtroller ($rootScope,$scope, $location, FoodSvc,$modal,$log){
        console.log("foodCtroller starting .......");
        console.log($rootScope.merchant)
        var fm = this;
        fm.foods = [];
        FoodSvc.getMerchantId(function(err,data){
            if(!err){
                FoodSvc.getFoodlistByMerchantId($rootScope.merchant._id,function(err,data){
                    fm.foods = data;
                });
            }
        })
       
        fm.refresh = function(){
            FoodSvc.getFoodlistByMerchantId($rootScope.merchant._id,function(err,data){
                fm.foods = data;
            });
        }
        fm.update = function(item){
            $scope.food = item; 
            console.log(item._id);
            // let modalInstance = $modal.open({
            //     templateUrl : require('../html/updateFood.html')
            // })
            var modalInstance = $modal.open({  
                template : require('../html/updateFood.html'),  
                controller : updateFoodController,  
                resolve : {  
                    food : function() {
                        return item;  
                    },
                    refreshFood : function(){
                        return fm.refresh;
                    }  
                }  
            });  
            
            modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
                console.log('modal is opened');
                 
                console.log($scope.food);
            });  
            modalInstance.result.then(function(result) {  
                console.log(result);  
            }, function(reason) {  
                console.log(reason);// 点击空白区域，总会输出backdrop  
                // click，点击取消，则会暑促cancel  
                $log.info('Modal dismissed at: ' + new Date());  
            }); 

        }

        
        fm.delete = function(item){
            console.log(item._id);
            FoodSvc.deleteFood(item._id,function(err,result){
                if(err){
                    console.log(err);
                }else{
                    fm.refresh();
                }
            })
        }

        
        fm.add = function(){

            var modalInstance = $modal.open({  
                template : require('../html/addFood.html'),  
                controller : addFoodController,
                resolve : {  
                    foods : function() { 
                        return fm.foods;  
                    },
                    refreshFood : function(){
                        return fm.refresh;
                    }   
                }  
            });  
            
            modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
                console.log('modal is opened');
            });  
            modalInstance.result.then(function(result) {  
                console.log(result);  
            }, function(reason) {  
                console.log(reason);// 点击空白区域，总会输出backdrop  
                // click，点击取消，则会暑促cancel  
                $log.info('Modal dismissed at: ' + new Date());  
            }); 

        }

 }

 module.exports = foodCtroller;